﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Moon.DBUtility;

namespace DAL
{
	/// <summary>
	/// 数据访问类:t_user
	/// </summary>
    public partial class UserDAL
    {
        public UserDAL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_user");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_user(");
            strSql.Append("AdminUser,AdminPass,IpLog)");
            strSql.Append(" values (");
            strSql.Append("@AdminUser,@AdminPass,@IpLog)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@AdminUser", OleDbType.VarChar,255),
					new OleDbParameter("@AdminPass", OleDbType.VarChar,255),
					new OleDbParameter("@IpLog", OleDbType.VarChar,255)};
            parameters[0].Value = model.AdminUser;
            parameters[1].Value = model.AdminPass;
            parameters[2].Value = model.IpLog;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_user set ");
            strSql.Append("AdminUser=@AdminUser,");
            strSql.Append("AdminPass=@AdminPass,");
            strSql.Append("IpLog=@IpLog");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@AdminUser", OleDbType.VarChar,255),
					new OleDbParameter("@AdminPass", OleDbType.VarChar,255),
					new OleDbParameter("@IpLog", OleDbType.VarChar,255),
					new OleDbParameter("@Id", OleDbType.Integer,4)};
            parameters[0].Value = model.AdminUser;
            parameters[1].Value = model.AdminPass;
            parameters[2].Value = model.IpLog;
            parameters[3].Value = model.Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user ");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_user ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.UserInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,AdminUser,AdminPass,IpLog from t_user ");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            Model.UserInfo model = new Model.UserInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AdminUser"] != null && ds.Tables[0].Rows[0]["AdminUser"].ToString() != "")
                {
                    model.AdminUser = ds.Tables[0].Rows[0]["AdminUser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AdminPass"] != null && ds.Tables[0].Rows[0]["AdminPass"].ToString() != "")
                {
                    model.AdminPass = ds.Tables[0].Rows[0]["AdminPass"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IpLog"] != null && ds.Tables[0].Rows[0]["IpLog"].ToString() != "")
                {
                    model.IpLog = ds.Tables[0].Rows[0]["IpLog"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,AdminUser,AdminPass,IpLog ");
            strSql.Append(" FROM t_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
        {
            //獲得查詢條數
            int count = endIndex - startIndex;
            //查詢條件
            string where = "";
            //內排序條件
            string Inorderby = "order by UsersID desc";
            //外排序條件
            string Outorderby = "order by UsersID ASC";
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                where += " where 1=1 " + strWhere;
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }
            else
            {
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from (select top {0} * from (select top {1} * from t_Admin {2} {3} ) {4}) {3}", count, endIndex, where, Inorderby, Outorderby);

            return DbHelperOleDb.Query(strSql.ToString());
        }
        /*	public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
            {
                StringBuilder strSql=new StringBuilder();
                strSql.Append("SELECT * FROM ( ");
                strSql.Append(" SELECT ROW_NUMBER() OVER (");
                if (!string.IsNullOrEmpty(orderby.Trim()))
                {
                    strSql.Append("order by T." + orderby );
                }
                else
                {
                    strSql.Append("order by T.Id desc");
                }
                strSql.Append(")AS Row, T.*  from t_user T ");
                if (!string.IsNullOrEmpty(strWhere.Trim()))
                {
                    strSql.Append(" WHERE " + strWhere);
                }
                strSql.Append(" ) TT");
                strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
                return DbHelperOleDb.Query(strSql.ToString());
            }

            /*
            /// <summary>
            /// 分页获取数据列表
            /// </summary>
            public DataSet GetList(int PageSize,int PageIndex,string strWhere)
            {
                OleDbParameter[] parameters = {
                        new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                        new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                        new OleDbParameter("@PageSize", OleDbType.Integer),
                        new OleDbParameter("@PageIndex", OleDbType.Integer),
                        new OleDbParameter("@IsReCount", OleDbType.Boolean),
                        new OleDbParameter("@OrderType", OleDbType.Boolean),
                        new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                        };
                parameters[0].Value = "t_user";
                parameters[1].Value = "Id";
                parameters[2].Value = PageSize;
                parameters[3].Value = PageIndex;
                parameters[4].Value = 0;
                parameters[5].Value = 0;
                parameters[6].Value = strWhere;	
                return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
            }*/
    }
		#endregion  Method
}
